package org.javaboy.demo;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.elasticsearch.indices.DeleteIndexResponse;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;

import java.io.IOException;
import java.util.List;

public class Client04 {
    public static void main(String[] args) throws IOException {
        RestClient client = RestClient.builder(new HttpHost("localhost", 9200, "http")).build();
        RestClientTransport transport = new RestClientTransport(client, new JacksonJsonpMapper());
        ElasticsearchClient es = new ElasticsearchClient(transport);
        SearchResponse<Book> resp = es.search(s -> s.index("books").query(q -> q.term(t -> t.field("name").value("java"))), Book.class);
        List<Hit<Book>> hits = resp.hits().hits();
        for (Hit<Book> hit : hits) {
            System.out.println("hit = " + hit);
        }
    }
}
